package core.resources
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.ProgressEvent;

import utils.Tracer;

/**
 * ResourceManagerBase
 * @author jliang
 * 
 */
public class ResourceManagerBase extends EventDispatcher
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function ResourceManagerBase()
	{
		
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	protected var loadedCount:int = 0;
	
	protected var taskList:Array = [];
	
	public var params:* = {};
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function start():void
	{
		startTask();
	}
	
	public function dispose():void
	{
		for each (var manager:LoadTaskBase in taskList)
		{
			if (!manager) continue;
			removeTask(manager);
			manager.dispose();
			manager = null;
		}
		taskList = null;
	}
	//==========================================================================
	//  Private methods
	//==========================================================================
	protected function addTask(value:LoadTaskBase):void
	{
		value.params = params;
		value.addEventListener(ResourceEvent.RESOURCE_LOADING, resourceProgressHandler);
		value.addEventListener(Event.COMPLETE, resourceCompleteHandler);
		taskList.push(value);
	}
	
	protected function removeTask(value:LoadTaskBase):void
	{
		value.removeEventListener(ResourceEvent.RESOURCE_LOADING, resourceProgressHandler);
		value.removeEventListener(Event.COMPLETE, resourceCompleteHandler);
	}
	
	protected function startTask():void
	{
		var manager:LoadTaskBase = LoadTaskBase(taskList[loadedCount]);
		if (manager) manager.start();
	}
	
	protected function updateProgress():void
	{
		var evt:ProgressEvent = new ProgressEvent(ProgressEvent.PROGRESS);
		evt.bytesLoaded = loadedCount;
		evt.bytesTotal = taskList.length;
		this.dispatchEvent(evt);
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================	
	protected function resourceProgressHandler(event:ResourceEvent):void
	{    	
		var evt:ProgressEvent = new ProgressEvent(ProgressEvent.PROGRESS);
		evt.bytesLoaded = 0.5 * event.bytesLoaded;
		evt.bytesTotal = event.bytesTotal;
	}    
	
	private function resourceCompleteHandler(event:Event):void
	{
		removeTask(event.target as LoadTaskBase);
		
		loadedCount ++;
		
		Tracer.log("Preload task complete [" + loadedCount + "/" + taskList.length + "]: " + event.target.toString());
		
		updateProgress();
		if (loadedCount < taskList.length)
		{
			startTask();
		}
		else if (loadedCount >= taskList.length)
		{
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
	}  
}
}